package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.business.domain.SeckillVoucherDto;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.SeckillVoucherMapper;
import com.ruoyi.business.domain.SeckillVoucher;
import com.ruoyi.business.service.SeckillVoucherService;

/**

 * 秒杀优惠券Service业务层处理

 *

 * @author ruoyi

 * @date 2025-06-09
 */
@Service
public class SeckillVoucherServiceImpl implements SeckillVoucherService
{
    @Autowired
    private SeckillVoucherMapper tbSeckillVoucherMapper;

    /**

     * 查询秒杀优惠券
     *
     * @param voucherId 秒杀优惠券主键
     * @return 秒杀优惠券
     */
    @Override
    public SeckillVoucher selectTbSeckillVoucherByVoucherId(String voucherId)
    {
        return tbSeckillVoucherMapper.selectTbSeckillVoucherByVoucherId(voucherId);
    }

    /**

     * 查询秒杀优惠券列表
     *
     * @param seckillVoucher 秒杀优惠券
     * @return 秒杀优惠券
     */
    @Override
    public List<SeckillVoucherDto> selectTbSeckillVoucherList(SeckillVoucher seckillVoucher,Long userId)
    {
        List<SeckillVoucherDto> seckillVoucherDtoList = new ArrayList<>();
        List<SeckillVoucher> seckillVoucherList = tbSeckillVoucherMapper.selectTbSeckillVoucherList(seckillVoucher,userId);
        for(SeckillVoucher seckillVoucherOne : seckillVoucherList){
            SeckillVoucherDto seckillVoucherDto = new SeckillVoucherDto();
            String voucherId = seckillVoucherOne.getVoucherId();
            String voucherName = tbSeckillVoucherMapper.selectTbVoucherNameById(voucherId);
            seckillVoucherDto.setVoucherName(voucherName);
            seckillVoucherDto.setVoucherId(voucherId);
            seckillVoucherDto.setCreateTime(seckillVoucherOne.getCreateTime());
            seckillVoucherDto.setBeginTime(seckillVoucherOne.getBeginTime());
            seckillVoucherDto.setEndTime(seckillVoucherOne.getEndTime());
            seckillVoucherDto.setUpdateTime(seckillVoucherOne.getUpdateTime());
            seckillVoucherDto.setStock(seckillVoucherOne.getStock());
            seckillVoucherDtoList.add(seckillVoucherDto);
        }
        return seckillVoucherDtoList;
    }

    /**

     * 新增秒杀优惠券
     *
     * @param seckillVoucher 秒杀优惠券
     * @return 结果
     */
    @Override
    public int insertTbSeckillVoucher(SeckillVoucher seckillVoucher)
    {
        seckillVoucher.setCreateTime(DateUtils.getNowDate());
        seckillVoucher.setUpdateTime(DateUtils.getNowDate());
        return tbSeckillVoucherMapper.insertTbSeckillVoucher(seckillVoucher);
    }

    /**

     * 修改秒杀优惠券
     *
     * @param seckillVoucher 秒杀优惠券
     * @return 结果
     */
    @Override
    public int updateTbSeckillVoucher(SeckillVoucher seckillVoucher)
    {
        seckillVoucher.setUpdateTime(DateUtils.getNowDate());
        seckillVoucher.setCreateTime(DateUtils.getNowDate());
        return tbSeckillVoucherMapper.updateTbSeckillVoucher(seckillVoucher);
    }

    /**

     * 批量删除秒杀优惠券
     *
     * @param voucherIds 需要删除的秒杀优惠券主键
     * @return 结果
     */
    @Override
    public int deleteTbSeckillVoucherByVoucherIds(String[] voucherIds)
    {
        return tbSeckillVoucherMapper.deleteTbSeckillVoucherByVoucherIds(voucherIds);
    }

    /**

     * 删除秒杀优惠券信息
     *
     * @param voucherId 秒杀优惠券主键
     * @return 结果
     */
    @Override
    public int deleteTbSeckillVoucherByVoucherId(String voucherId)
    {
        return tbSeckillVoucherMapper.deleteTbSeckillVoucherByVoucherId(voucherId);
    }
}